<?php
/**
 * Pertyler DatabaseManagement
 * Control
 * RowUnderRowArray
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require_once __DIR__ . '/main.php';

if(isset($_POST['Update']))
	return require __DIR__ . '/UpdateRow.php';

if(!isset($_GET['Row']))
	throw new \Exception('Row parameter required.');

if(!isset($_GET['Table']))
	throw new \Exception('Table parameter required.');

require_once __DIR__ . '/../databaseModel/Role.php';

if(!($moderator = Role_Moderator::Select()))
	return require __DIR__ . '/Index.php';

require_once __DIR__ . '/../databaseModel/Table.php';

if(!($table = Table::Select($_GET['Table'])))
	throw new \Exception('Table cannot found.');

if(!$table->SelectPrivilege('select'))
	throw new \Exception('Table does not have select privilege.');

if(!Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select')))
	throw new \Exception('Select privilege required.');

$table = $table->SelectParentTable();

if(!($row = $table->SelectRow($_GET['Row'])))
	throw new \Exception('Row cannot found.');

$table = $row->SelectTable($_GET['Table']);

require_once __DIR__ . '/../library/Pert/General/Page.php';

\Pert\Page::Get()->AddTitle($table);

$Content = \Pert\Page::Get()->Content();

$Content->RowName = (string) $row;

$Content->TableName = (string) $table;

$Content->TableId = $table->Id();

$publishedRowArray = $table->SelectPublishedRowArray();

$publishedRowColumnValueArrayArray = array();

foreach($publishedRowArray AS $publishedRow)
	$publishedRowColumnValueArrayArray[$publishedRow->Id()][] = (string) $publishedRow;

unset($publishedRowArray);

unset($publishedRow);

$Content->PublishedRowColumnValueArrayArray = $publishedRowColumnValueArrayArray;

unset($publishedRowColumnValueArrayArray);

if($table->HasUnpublishedColumn())
{
	$unpublishedRowArray = $table->SelectUnpublishedRowArray();

	$unpublishedRowColumnValueArrayArray = array();

	foreach($unpublishedRowArray AS $unpublishedRow)
		$unpublishedRowColumnValueArrayArray[$unpublishedRow->Id()][] = (string) $unpublishedRow;

	unset($unpublishedRowArray);

	unset($unpublishedRow);

	$Content->UnpublishedRowColumnValueArrayArray = $unpublishedRowColumnValueArrayArray;

	unset($unpublishedRowColumnValueArrayArray);
}

if($table->SelectPrivilege('update'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('update')))
	{
		$Content->UpdatePrivilege = true;

		if($table->HasUnpublishedColumn())
		{
			$Content->UpdateAsUnpublishedPrivilege = true;

			$Content->UpdateAsPublishedPrivilege = true;
		}
	}

if($table->SelectPrivilege('approve'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('approve')))
		$Content->ApprovePrivilege = true;

if($table->SelectPrivilege('delete'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('delete')))
		if($table->HasUnpublishedColumn())
			$Content->DeleteUnpublishedPrivilege = true;
		else
			$Content->DeletePrivilege = true;

if($table->SelectPrivilege('insert'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('insert')))
		$Content->InsertPrivilege = true;

unset($row);

unset($table);

unset($_GET);

unset($_POST);

echo \Pert\Page::Get()->HeaderHTML();

require __DIR__ . '/../view/RowUnderRowArray.php';

require __DIR__ . '/../view/PublishedRowArrayGrid.php';

if($Content->UnpublishedRowColumnValueArrayArray)
	require __DIR__ . '/../view/UnpublishedRowArrayGrid.php';

echo \Pert\Page::Get()->FooterHTML();

return true;
